import { toRaw } from 'vue';
import { CommonReqParam } from '@/domain/dataBoard/types/common';
import { CHILD_ID, PAGE } from '@/domain/dataBoard/variable/common';
import { Page, selectParam } from '@/domain/common/types/page';
export type SearchInfo = <T>(param: T, rowlist: any, loadTable: any, searchParam: selectParam) => void;
// 查询
export const searchInfo: SearchInfo = <T>(param: T, rowlist: any, loadTable: any, searchParam: selectParam) => {
  const params: Partial<T> = {};
  for (let key in param) {
    if (Array.isArray(param[key])) {
      (params[key] as string) = (param[key] as Array<number>).join('|');
    } else {
      params[key] = param[key];
    }
  }
  getTableList<T>(rowlist, loadTable, searchParam, params);
};
// 搜索重置
type ResetSearch = <T, P>(params: Partial<T>, reset: Function, searchParam: selectParam) => void;
export const resetSearch: ResetSearch = <T, P>(params: Partial<T>, reset: Function, searchParam: selectParam) => {
  reset();
  Object.keys(searchParam).forEach((item) => {
    if (item !== CHILD_ID) {
      delete searchParam[item as keyof Partial<Page | CommonReqParam | Partial<P & Page>>];
    }
  });
  searchParam[PAGE.PAGE] = 1;
  searchParam[PAGE.PAGE_SIZE] = 10;
};
// 列表接口请求
export const getTableList = async <T>(rowlist: any, loadTable: any, searchParam: selectParam, params: Partial<T>, value?: string | undefined) => {
  // 排序更新
  if (typeof params === 'string') {
    rowlist.value = (await loadTable({ ...searchParam, [params]: value })).data.datas;
  } else {
    // 查询更新
    rowlist.value = (await loadTable(toRaw(params))).data.datas;
  }
};
